package www.dianbaozu.com.ui.server

import android.view.View
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentCreateServerBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.openPhotoAlbumAgency
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper.MMKV_ID
import www.wen.com.common.lib.helper.loading.KeyHelper.REFRESHING_SERVER_LIST
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.SpUtil
import java.io.File

/**
 * @author WEN
 * @Description:创建新的服务器
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class CreateServerFragment : BaseLoadingFragment<FragmentCreateServerBinding>(), View.OnClickListener {

    private lateinit var imgFile: File

    private val createServerViewModel: CreateServerViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = createServerViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            getString(R.string.create_server),
            action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_create_server

    override fun lazyInitView() {
        dataBinding.run {
            viewModel = createServerViewModel
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvCreateServer -> {
                if(!this@CreateServerFragment::imgFile.isInitialized){
                    toast("请上传头像")
                    return
                }
                if(dataBinding.etInputServerName.trimProxy().isBlank()){
                    toast("请填写服务器名称")
                    return
                }
                createServerViewModel.requestCreateServer(
                        partMap = mapOf("userId" to SpUtil.decodeString(MMKV_ID)!!.toRequestBody("text/plain".toMediaTypeOrNull())),
                        serverName = dataBinding.etInputServerName.text.toString(),
                        MultipartBody.Part.createFormData("files", imgFile.name, imgFile.asRequestBody("image/jpeg".toMediaTypeOrNull()))
                ){
                    toast("创建成功")
                    postObject(REFRESHING_SERVER_LIST,true)
                    actionLeft()
                }
            }
            R.id.tvErrorTip -> {
                createServerViewModel.mIsShowErrorTip.set(View.INVISIBLE)
            }
            R.id.slUploadAvatar -> {
                openPhotoAlbumAgency(size = 400) {
                    val imgLocalFile = if(it[0].androidQToPath.isNullOrBlank()) File(it[0].compressPath) else File(it[0].androidQToPath)
                    if (imgLocalFile.exists()) {
                        imgFile = imgLocalFile
                        createServerViewModel.mAvatar = imgFile.absolutePath
                        setImageUrl(dataBinding.ivAvatar, imgFile.absolutePath)
                        dataBinding.strUpLoadText.visibility = View.GONE
                        createServerViewModel.checkCanCreate()
                    }
                }
            }
        }
    }
}



